package other;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class HDClientSocket {

	private final static String TAG = "TCP";

	private InputStream mInputStream;
	/**
	 * 接收文件的Socket的输入输出流
	 */
	private Socket mSocket;
	/**
	 * 文件接收监听事件
	 */
	private OnSendListener mOnSendListener;

	private FileOutputStream mFileOutputStream;
	private InputStreamReader mInputStreamReader;
	private BufferedReader mBufferedReader;
	private OutputStream mOutputStream;
	private FileInputStream mFileInputStream;
	private OutputStreamWriter mOutputStreamWriter;
	private BufferedWriter mBufferedWriter;

	public HDClientSocket(String ipAddress, int port, OnSendListener onSendListener) {

		try {

			mOnSendListener = onSendListener;
			//1.创建监听指定服务器地址以及指定服务器监听的端口号
			mSocket = new Socket(ipAddress, port);

		} catch (Exception e) {
			if (mOnSendListener != null) {
				mOnSendListener.onFailure(e);
			}
		}
	}


	private void sendFileData(String filePath) {
		//发送文件实体数据
		try {

			if (mSocket != null) {

				//2.拿到客户端的socket对象的输出流发送给服务器数据
				mOutputStream = mSocket.getOutputStream();

				mFileInputStream = new FileInputStream(filePath);
				int size = -1;
				byte[] buffer = new byte[1024];
				while ((size = mFileInputStream.read(buffer, 0, 1024)) != -1) {
					//写入要发送给服务器的数据
					mOutputStream.write(buffer, 0, size);
				}

				//文件接收成功
				if (mOnSendListener != null) {
					mOnSendListener.onSuccess(filePath);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			if (mOnSendListener != null) {
				mOnSendListener.onFailure(e);
			}
		} finally {

			try {
				if (mSocket != null && mSocket.isConnected() && mOutputStream != null
						&& mFileInputStream != null) {
					// 关闭输出流
					mSocket.shutdownOutput();
					mOutputStream.close();
					mFileInputStream.close();
					mSocket.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				if (mOnSendListener != null) {
					mOnSendListener.onFailure(e);
				}
			}

		}
	}

	private String sendData(String data) {

		String result = null;

		try {

			if (mSocket != null ) {

				//2.拿到客户端的socket对象的输出流发送给服务器数据
				mOutputStream = mSocket.getOutputStream();

				mOutputStreamWriter = new OutputStreamWriter(mOutputStream);
				mBufferedWriter = new BufferedWriter(mOutputStreamWriter);
				//写入要发送给服务器的数据
				mBufferedWriter.write(data);

				//文件接收成功
				if (mOnSendListener != null) {
					mOnSendListener.onSuccess(data);
				}
			}


		} catch (Exception e) {
			e.printStackTrace();
			if (mOnSendListener != null) {
				mOnSendListener.onFailure(e);
			}
		} finally {

			try {
				if (mSocket != null && mSocket.isConnected() && mOutputStream != null
						&& mOutputStreamWriter != null && mBufferedWriter != null) {
					// 关闭输出流
					mSocket.shutdownOutput();
					mOutputStream.close();
					mOutputStreamWriter.close();
					mBufferedWriter.close();

					mSocket.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				if (mOnSendListener != null) {
					mOnSendListener.onFailure(e);
				}
			}

		}

		return result;

	}


	public interface OnSendListener {
		void onSuccess(String callBack);
		void onFailure(Throwable throwable);
	}

}
